package edu.ynu.dao;
// default package

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import edu.ynu.po.Enterprise;

/**
 	* A data access object (DAO) providing persistence and search support for Enterprise entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see .Enterprise
  * @author MyEclipse Persistence Tools 
 */

public class EnterpriseDAO extends BaseHibernateDAO  {
		 private static final Log log = LogFactory.getLog(EnterpriseDAO.class);
		//property constants
	public static final String BUSINESS = "business";
	public static final String SCALE = "scale";
	public static final String CHEIFTAIN = "cheiftain";
	public static final String LICENCE_NO = "licenceNo";
	public static final String BANKROLL = "bankroll";
	public static final String TURNOVER = "turnover";
	public static final String BANK = "bank";
	public static final String BANK_ACCOUNT = "bankAccount";
	public static final String LOCAL_TAX_NO = "localTaxNo";
	public static final String NATIONAL_TAX_NO = "nationalTaxNo";
	public static final String INTRODUCTION = "introduction";



    
    public void save(Enterprise transientInstance) {
        log.debug("saving Enterprise instance");
        try {
        	Session session = getSession();  
    		Transaction trans = session.beginTransaction();  
    		session.save(transientInstance);  
    		trans.commit();  
    		session.close(); 
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(Enterprise persistentInstance) {
        log.debug("deleting Enterprise instance");
        try {
        	Session session = getSession();  
    		Transaction trans = session.beginTransaction();  
            session.delete(persistentInstance);
            trans.commit();  
    		session.close();
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
	
	public void update (Enterprise e) {
		Session session = getSession();  
		Transaction trans = session.beginTransaction();  
		session.update(e);  
		trans.commit();  
		session.close(); 
	}
	
	public void saveOrUpdate(Enterprise enter) {
		Session session = getSession();  
		Transaction trans = session.beginTransaction();  
		session.saveOrUpdate(enter);  
		trans.commit();  
		session.close();
	}
    
    public Enterprise findById( java.lang.String id) {
        log.debug("getting Enterprise instance with id: " + id);
        try {
            Enterprise instance = (Enterprise) getSession()
                    .get("edu.ynu.po.Enterprise", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(Enterprise instance) {
        log.debug("finding Enterprise instance by example");
        try {
            List results = getSession()
                    .createCriteria("Enterprise")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding Enterprise instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from Enterprise as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByBusiness(Object business
	) {
		return findByProperty(BUSINESS, business
		);
	}
	
	public List findByScale(Object scale
	) {
		return findByProperty(SCALE, scale
		);
	}
	
	public List findByCheiftain(Object cheiftain
	) {
		return findByProperty(CHEIFTAIN, cheiftain
		);
	}
	
	public List findByLicenceNo(Object licenceNo
	) {
		return findByProperty(LICENCE_NO, licenceNo
		);
	}
	
	public List findByBankroll(Object bankroll
	) {
		return findByProperty(BANKROLL, bankroll
		);
	}
	
	public List findByTurnover(Object turnover
	) {
		return findByProperty(TURNOVER, turnover
		);
	}
	
	public List findByBank(Object bank
	) {
		return findByProperty(BANK, bank
		);
	}
	
	public List findByBankAccount(Object bankAccount
	) {
		return findByProperty(BANK_ACCOUNT, bankAccount
		);
	}
	
	public List findByLocalTaxNo(Object localTaxNo
	) {
		return findByProperty(LOCAL_TAX_NO, localTaxNo
		);
	}
	
	public List findByNationalTaxNo(Object nationalTaxNo
	) {
		return findByProperty(NATIONAL_TAX_NO, nationalTaxNo
		);
	}
	
	public List findByIntroduction(Object introduction
	) {
		return findByProperty(INTRODUCTION, introduction
		);
	}
	

	public List findAll() {
		log.debug("finding all Enterprise instances");
		try {
			String queryString = "from Enterprise";
	         Query queryObject = getSession().createQuery(queryString);
			 return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public Enterprise merge(Enterprise detachedInstance) {
        log.debug("merging Enterprise instance");
        try {
            Enterprise result = (Enterprise) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Enterprise instance) {
        log.debug("attaching dirty Enterprise instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(Enterprise instance) {
        log.debug("attaching clean Enterprise instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}